package com.example.project7.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.project7.R;
import com.example.project7.model.Book;
import java.util.ArrayList;
import java.util.List;

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.ViewHolder> {
    private List<Book> bookList;
    private OnBookClickListener onBookClickListener;
    private OnBookLongClickListener onBookLongClickListener;

    public BookAdapter() {
        this.bookList = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_book, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Book book = bookList.get(position);
        holder.tvBookNumber.setText("书号：" + book.getBookNumber());
        holder.tvBookName.setText("书名：" + book.getBookName());
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    /**
     * 更新数据
     * @param newList 新的数据列表
     */
    public void updateData(List<Book> newList) {
        this.bookList = newList != null ? newList : new ArrayList<>();
        notifyDataSetChanged();
    }

    /**
     * 设置点击监听器
     * @param listener 点击监听器
     */
    public void setOnBookClickListener(OnBookClickListener listener) {
        this.onBookClickListener = listener;
    }

    /**
     * 设置长按监听器
     * @param listener 长按监听器
     */
    public void setOnBookLongClickListener(OnBookLongClickListener listener) {
        this.onBookLongClickListener = listener;
    }

    /**
     * 获取指定位置的书籍
     * @param position 位置
     * @return Book
     */
    public Book getBook(int position) {
        if (position >= 0 && position < bookList.size()) {
            return bookList.get(position);
        }
        return null;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvBookNumber;
        TextView tvBookName;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvBookNumber = itemView.findViewById(R.id.tv_book_number);
            tvBookName = itemView.findViewById(R.id.tv_book_name);

            // 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && onBookClickListener != null) {
                        onBookClickListener.onBookClick(position, bookList.get(position));
                    }
                }
            });

            // 设置长按事件
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && onBookLongClickListener != null) {
                        return onBookLongClickListener.onBookLongClick(position, bookList.get(position));
                    }
                    return false;
                }
            });
        }
    }

    /**
     * 书籍点击监听器接口
     */
    public interface OnBookClickListener {
        void onBookClick(int position, Book book);
    }

    /**
     * 书籍长按监听器接口
     */
    public interface OnBookLongClickListener {
        boolean onBookLongClick(int position, Book book);
    }
}